package com.cykj.user.service.impl;

import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.FileResumeDO;
import com.cykj.common.entity.UserDO;
import com.cykj.user.mapper.UploadFileMapper;
import com.cykj.user.mapper.UserMapper;
import com.cykj.user.service.IUploadFileService;
import com.cykj.user.vo.FileResumeVO;
import com.cykj.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.user.service.impl
 * @Author: 浮行
 * @CreateTime: 2025-08-20 09:20
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UploadFileServiceImpl implements IUploadFileService {
    @Value("${fileUrl}")
    private String fileUrl;
    @Autowired
    private UploadFileMapper uploadFileMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public ResponseDTO delete(FileResumeVO fileResumeVO) {
        Integer res = uploadFileMapper.delete(fileResumeVO);
        if(res > 0){
            return ResponseDTO.success();
        }
        return ResponseDTO.error();
    }

    @Override
    public ResponseDTO resumeList(FileResumeVO fileResumeVO) {
        List<FileResumeDO> fileResumeDOS = uploadFileMapper.resumeList(fileResumeVO);
        if(fileResumeDOS != null){
            for (FileResumeDO fileResumeDO : fileResumeDOS) {
                fileResumeDO.setFileUrl(fileUrl+fileResumeDO.getFileUrl());
            }
            return ResponseDTO.success(fileResumeDOS);
        }
        return ResponseDTO.error();
    }

    @Override
    public ResponseDTO uploadResume(Integer userId, String fileUrl) {
        Integer res = uploadFileMapper.uploadResume(userId, fileUrl);
        if(res > 0){
            return ResponseDTO.success();
        }
        return ResponseDTO.error();
    }

    @Override
    public ResponseDTO updateHeadIcon(UserVO userVO) {
        Integer res = uploadFileMapper.updateHeadIcon(userVO);
        if(res > 0){
            UserDO EphemeralData = userMapper.getEphemeralData(userVO.getUserId());
            if(EphemeralData.getHeadIcon() != null){
                //拼接头像url+相对路径
                EphemeralData.setHeadIcon(fileUrl + EphemeralData.getHeadIcon());
            }
            redisTemplate.opsForValue().set("user:"+userVO.getUserId(),EphemeralData);
            return ResponseDTO.success();
        }
        return ResponseDTO.error();
    }
}
